package com.jack.blog.dto;

import com.jack.blog.entity.Category;
import com.jack.blog.vo.CategoryVo;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Component
public class CategoryDto extends BaseDto{

    public CategoryVo CategoryToCategoryVo(Category category){
        com.jack.blog.vo.CategoryVo categoryVo=new com.jack.blog.vo.CategoryVo();
        categoryVo.setId(category.getId());
        categoryVo.setValue(category.getValue());
        categoryVo.setIntroduction(category.getIntroduction());
        categoryVo.setBlog(category.getBlog());
        return categoryVo;
    }

    public List<CategoryVo> CategoryToCategoryVo(List<Category> categories){
        List<CategoryVo> categoryVos=new LinkedList<>();
        for (Category category:categories)
            categoryVos.add(CategoryToCategoryVo(category));
        return categoryVos;
    }

    public Category CategoryVoToCategory(int uid, CategoryVo categoryVo)throws IllegalArgumentException{
        Category category=new Category();
        String value = categoryVo.getValue();
        if (value.length()>10)
            throw new IllegalArgumentException("分栏标题长度超过10!");
        String introduction = categoryVo.getIntroduction();
        if (introduction.length()>50)
            throw new IllegalArgumentException("分栏简介长度超过50!");
        category.setValue(value);
        category.setIntroduction(introduction);
        category.setUid(uid);
        category.setId(categoryVo.getId());
        return category;
    }

}
